﻿using Newtonsoft.Json;
using System;

namespace DotNetty.Framework
{
    public abstract class AbstractMessage
    {
        /// <summary>
        /// 消息唯一Token
        /// </summary>
        public string Token { get; set; }


        /// <summary>
        /// 接收用户
        /// </summary>
        public string ToChannel { get; set; }

        /// <summary>
        /// Command命令
        /// </summary>
        public string CommandText { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; }

        /// <summary>
        /// 是否是群组
        /// </summary>
        public bool IsGroup { get; set; }


        /// <summary>
        /// 任务开始时间
        /// </summary>
        public DateTime TaskStartDateTime { get; set; }

        /// <summary>
        /// 声明处理Hander
        /// </summary>
        public ChanneHandelEnum Handel { get; set; }

        /// <summary>
        /// 消息是否必须到达
        /// 默认为 “是”
        /// 如果为 “否” 则不存在订阅消息时抛弃消息
        /// </summary>
        public bool IsMustBeServed { get; set; } = true;

        /// <summary>
        /// 参数
        /// </summary>
        public string Parameter { get; set; }

        //[JsonIgnore]
        //private object _parameter { get; set; }

        /// <summary>
        /// 获取参数模型
        /// </summary>
        /// <returns></returns>
        public T GetParameterModel<T>()
            where T : class
        {
            if (!string.IsNullOrWhiteSpace(this.Parameter))
            {
                return JsonConvert.DeserializeObject<T>(this.Parameter);
                //if (_parameter == null)
                //{
                //    _parameter = JsonConvert.DeserializeObject<T>(this.Parameter);
                //}

                //return (T)_parameter;
            }

            return null;
        }
    }
}
